package com.jiyun.service.impl;

import com.alibaba.nacos.api.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.QuizMapper;
import com.jiyun.mapper.TrainMapper;
import com.jiyun.model.Course;
import com.jiyun.model.Quiz;
import com.jiyun.model.Train;
import com.jiyun.service.TrainService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train> implements TrainService {

    @Autowired
    TrainMapper trainMapper;

    @Autowired
    QuizMapper quizMapper;



    @Override
    public List<Train> trainCourse() {
        ArrayList<Train> list = new ArrayList<>();
        LambdaQueryWrapper<Train> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Train::getStarttime);
        List<Train> list1 = trainMapper.selectList(wrapper);
        list.clear();
        for (int i = 0; i < 3; i++) {
            list.add(list1.get(i));
        }
        for (Train train : list1) {
            Date date = new Date();
            if (date.getTime()>=train.getStarttime().getTime()&&date.getTime()<=train.getEndtime().getTime()){
                train.setStatus("进行中");
            }else if (date.getTime()<train.getStarttime().getTime()){
                train.setStatus("未开始");
            }else if (date.getTime()>train.getEndtime().getTime()){
                train.setStatus("已结束");
            }
            String[] split = train.getCid().split(",");
            train.setCount(split.length);
            Integer quiz=trainMapper.findQuiz(train.getId());
            train.setQuiz(quiz);
        }
        return list;
    }
}
